package org.dkakunsi.aplikasitoko.report.data.adapter;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dkakunsi.aplikasitoko.data.entity.Kasir;
import org.dkakunsi.aplikasitoko.data.entity.Pembelian;
import org.dkakunsi.aplikasitoko.data.entity.Penjualan;
import org.dkakunsi.aplikasitoko.data.entity.Transaksi;
import org.dkakunsi.aplikasitoko.data.util.HibernateUtil;
import org.dkakunsi.aplikasitoko.report.data.access.ReportAccess;
import org.dkakunsi.aplikasitoko.report.data.entity.ReportKasirTransaksi;
import org.dkakunsi.aplikasitoko.report.data.entity.ReportPenjualan;
import org.dkakunsi.aplikasitoko.report.data.entity.ReportTransaksiPembelian;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class ReportDataAdapter implements ReportAccess {

	private Session session;
	@SuppressWarnings("unused")
	private Transaction tx;

	public ReportDataAdapter() {
		this.session = HibernateUtil.openSession();
		this.tx = session.beginTransaction();
	}
	
	public ReportDataAdapter(Session session) {
		this.session = session;
		this.tx = session.beginTransaction();
	}

	@SuppressWarnings("unchecked")
	@Override
	public JasperPrint getSalesReport(Object object) throws JRException {
		Transaksi transaksi = (Transaksi) object;
		if( transaksi instanceof Penjualan ) {
			List<ReportPenjualan> penjualanReport = session.createQuery("select tb.barang.nama as NAMA, tb.barang.hargaJual as HARGAJUAL, tb.jumlahBarang as JUMLAH, " +
					"tb.transaksi.jumlahTransaksi as JUMLAHTRANSAKSI, tb.transaksi.jumlahBayar as JUMLAHBAYAR from TransaksiBarang tb where tb.transaksi = :trans")
					.setParameter("trans", transaksi).setResultTransformer(Transformers.aliasToBean(ReportPenjualan.class)).list();

			InputStream is = ReportDataAdapter.class.getResourceAsStream("/org/dkakunsi/aplikasitoko/report/StrukPembayaran.jasper");
			
			Map<String,Object> parameters = new HashMap<String, Object>();
			parameters.put("kode", transaksi.getKode());

			return JasperFillManager.fillReport(is, parameters, new JRBeanCollectionDataSource(penjualanReport));
		} else { 
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public JasperPrint getKasirTransaksiReport(Object object) throws JRException {
		Kasir kasir = (Kasir) object;
		List<ReportKasirTransaksi> transaksiKasirReport = session.createQuery( "select t.kode as kodeTransaksi, t.jumlahTransaksi as jumlahTransaksi, t.pelaku.kode as kodeLog, " +
				"t.pelaku.tanggal as tanggalTransaksi from Transaksi t where t.pelaku.kasir = :kasir" )
				.setParameter( "kasir", kasir ).setResultTransformer( Transformers.aliasToBean( ReportKasirTransaksi.class ) ).list();

		InputStream is = ReportDataAdapter.class.getResourceAsStream( "/org/dkakunsi/aplikasitoko/report/KasirTransaksi.jasper" );
			
		Map<String,Object> parameters = new HashMap<String, Object>();
		parameters.put( "kode", kasir.getKode() );
		parameters.put( "nama", kasir.getNama() );

		return JasperFillManager.fillReport( is, parameters, new JRBeanCollectionDataSource( transaksiKasirReport ) );
	}

	@Override
	public JasperPrint getTransaksiBarangReport(Object object)
			throws JRException {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public JasperPrint getTransaksiPembelianReport(Object object)
			throws JRException {
		Pembelian transaksi = (Pembelian) object;
		List<ReportKasirTransaksi> transaksiKasirReport = session.createQuery( "select tb.barang.kode as kodeBarang, tb.barang.nama as namaBarang, tb.jumlahBarang as jumlahBarang, " +
				"tb.transaksi.jumlahTransaksi as pembayaran from TransaksiBarang tb where tb.transaksi.tanggalPembelian like :tanggal%" )
				.setParameter( "tanggal", transaksi.getTanggalPembelian() ).setResultTransformer( Transformers.aliasToBean( ReportTransaksiPembelian.class ) ).list();

		InputStream is = ReportDataAdapter.class.getResourceAsStream( "/reports/KasirTransaksi.jasper" );
		
		String date = transaksi.getTanggalPembelian().substring(0, 2) + "/" +transaksi.getTanggalPembelian().substring(2, 4);
		Map<String,Object> parameters = new HashMap<String, Object>();
		parameters.put( "date", date );

		return JasperFillManager.fillReport( is, parameters, new JRBeanCollectionDataSource( transaksiKasirReport ) );
	}

	@SuppressWarnings("unchecked")
	@Override
	public JasperPrint getTransaksiPenjualanReport(Object object)
			throws JRException {
		List<ReportKasirTransaksi> transaksiKasirReport = session.createQuery( "select t.pelaku.kode as kodeLog, t.pelaku.kasir.nama as namaKasir, t.jumlahTransaksi as jumlahPenjualan " +
				"from Transaksi t.log.tanggal=:tanggal" )
				.setParameter( "tanggal", object ).setResultTransformer( Transformers.aliasToBean( ReportTransaksiPembelian.class ) ).list();

		InputStream is = ReportDataAdapter.class.getResourceAsStream( "/reports/KasirTransaksi.jasper" );
		
		Map<String,Object> parameters = new HashMap<String, Object>();
		parameters.put( "date", object );

		return JasperFillManager.fillReport( is, parameters, new JRBeanCollectionDataSource( transaksiKasirReport ) );
	}

}
